﻿<div style="color:Black;background-color:White;"><pre>
<span style="color:Green;">#-- Public Module Functions</span>
<span style="color:Blue;">function</span> Exec
{
<span style="color:Green;">&lt;#
.SYNOPSIS
Helper function for executing command-line programs.

.DESCRIPTION
This is a helper function that runs a scriptblock and checks the PS variable $lastexitcode to see if an error occcured.
If an error is detected then an exception is thrown.  This function allows you to run command-line programs without
having to explicitly check fthe $lastexitcode variable.

.PARAMETER cmd
The scriptblock to execute.  This scriptblock will typically contain the command-line invocation.
Required

.PARAMETER errorMessage
The error message used for the exception that is thrown.
Optional

.EXAMPLE
exec { svn info $repository_trunk } &quot;Error executing SVN. Please verify SVN command-line client is installed&quot;

This example calls the svn command-line client.

.LINK
Assert
Invoke-psake
Task
Properties
Include
FormatTaskName
TaskSetup
TaskTearDown
#&gt;</span>
  [<span style="color:PowderBlue;">CmdletBinding</span>()]

  <span style="color:Blue;">param</span>(
    <span style="color:Gray;">[</span><span style="color:Teal;">Parameter(Position=0,Mandatory=1)</span><span style="color:Gray;">]</span><span style="color:Gray;">[</span><span style="color:Teal;">scriptblock</span><span style="color:Gray;">]</span><span style="color:OrangeRed;">$cmd</span>,
    <span style="color:Gray;">[</span><span style="color:Teal;">Parameter(Position=1,Mandatory=0)</span><span style="color:Gray;">]</span><span style="color:Gray;">[</span><span style="color:Teal;">string</span><span style="color:Gray;">]</span><span style="color:OrangeRed;">$errorMessage</span> <span style="color:Gray;">=</span> <span style="color:#A31515;">&quot;Error executing command: &quot;</span> <span style="color:Gray;">+</span> <span style="color:OrangeRed;">$cmd</span>
  )
     &amp; <span style="color:OrangeRed;">$cmd</span>
     <span style="color:Blue;">if</span> (<span style="color:OrangeRed;">$lastexitcode</span> <span style="color:Gray;">-ne</span> 0)
     {
        <span style="color:Blue;">throw</span> <span style="color:OrangeRed;">$errorMessage</span>
     }
}
</pre></div>